package com.dwjm.controller;


import com.dwjm.pojo.Student;
import com.dwjm.pojo.StudentVO;
import com.dwjm.service.StudentUserService;
import com.dwjm.utlis.PageBean;
import com.dwjm.utlis.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentUserService studentUserService;

    //用户登录
    @PostMapping("/login")
    public Result login(String username, String password) {
        //查询学生账号是否存在
        Student s =  studentUserService.getByUserName(username);
        if (s == null){
            return Result.error("学生账号不存在");
        }
        //学生状态是否为正常
        if (!Objects.equals(s.getStatus(), "正常")){
            return Result.error("该账号已被禁用，请联系管理员");
        }
        //存在判断密码是否匹配
        if (s.getPassword().equals(password)) {
            //密码正确生成token
            String token = studentUserService.login(s);
            // 构造返回数据 传入token和学生信息
            Map<String, Object> data = new HashMap<>();
            data.put("token", token);
            data.put("student", s);
            return Result.success(data);
        }
        return Result.error("密码错误");
    }

    //学生注册
    @PostMapping("/register")
    public Result register(@RequestBody Student student) {
        //判断学生信息是否为空
        if (student==null){
            return Result.error("注册信息为空");
        }
        studentUserService.register(student);
        return Result.success();
    }

    //用户分页查询 根据学生账号和生源地地址
    @GetMapping("/pageList")
    public Result<PageBean<StudentVO>> getUserList(Integer pageNum, Integer pageSize, String username, String address) {
        PageBean<StudentVO> list = studentUserService.getUserList(pageNum,pageSize,username,address);
        return Result.success(list);
    }

    //修改学生状态
    @PutMapping("/updateStatus")
    public Result updateStatus(@RequestBody Map<String, Object> requestParams) {
        String studentId = (String) requestParams.get("studentId");
        String status = (String) requestParams.get("status");

        if (studentId == null) {
            return Result.error("学生账号不能为空");
        }
        if (status == null) {
            return Result.error("状态不能为空");
        }

        studentUserService.UpdateStatusByStudentId(studentId, status);
        return Result.success();
    }
    //更新学生信息
    @PutMapping("/update")
    public Result updateUser(@RequestBody Student student) {
        studentUserService.update(student);
        return Result.success("操作成功");
    }
    //添加用户
    @PostMapping("/add")
    public Result addUser(@RequestBody Student student) {
        //账号唯一性？
        Student s = studentUserService.getByUserName(student.getUsername());
        if (s != null) {
            return Result.error("账号已经存在");
        }
        studentUserService.add(student);
        return Result.success();
    }
}
